package com.igeek.boot.config;

import com.igeek.boot.entity.User;
import com.igeek.boot.exception.WisdomFactoryException;
import com.igeek.boot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Objects;

/**
 * TODO
 *
 *
 * UserDetailsService接口  SpringSecurity提供的关于认证的接口
 *
 */
@Service
public class SpringSecurityDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findUserByUsername(username);
        if(Objects.isNull(user)){
            throw new WisdomFactoryException("用户认证失败~");
        }

        //封装LoginUser
        LoginUser loginUser = new LoginUser(user);
        return loginUser;
    }
}
